package com.midea.ai.air.ui.more;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.common.ActionBarBackFragment;
import com.midea.ai.air.common.BaseActivity;
import com.midea.ai.air.common.ConsVal;
import com.midea.ai.air.common.Constant;
import com.midea.ai.air.utils.ComUtils;

/**
 * Created by ouyangdi on 2016/4/12.
 * 个人资料->用户名activity
 */
public class MorePersonNameActivity extends BaseActivity {
    ActionBarBackFragment mActionBar;
    private TextView mActionBarRightBtn;
    private String mActionBarTitle;

    private EditText mInputName;
    private ImageView mIvDelete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_more_person_name);

        mActionBarTitle = getIntent().getStringExtra(ConsVal.KEY_TITLE);
        initView();
    }

    private void initView() {
        initActionBar();
        mIvDelete = (ImageView) findViewById(R.id.iv_delete);
        mInputName = (EditText) findViewById(R.id.edit_input_name);
        mInputName.setSelection(mInputName.getText().length());
        mInputName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(!hasFocus) {
                    mIvDelete.setVisibility(View.GONE);
                } else if(!TextUtils.isEmpty(mInputName.getText())) {
                    mIvDelete.setVisibility(View.VISIBLE);
                }
            }
        });

        mIvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mInputName.setText("");
            }
        });

        mInputName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(TextUtils.isEmpty(s)) {
                    mActionBarRightBtn.setAlpha(0.25f);
                    mIvDelete.setVisibility(View.GONE);
                    return;
                }
                mIvDelete.setVisibility(View.VISIBLE);
                mActionBarRightBtn.setAlpha(1f);
                mInputName.setTextColor(getResources().getColor(R.color.text_black_standard));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void initActionBar() {
        FrameLayout mBarLayout = (FrameLayout)findViewById(R.id.bar_parent);
        ViewGroup.LayoutParams LP = mBarLayout.getLayoutParams();
        LP.height = Constant.getTopMargin() + getResources().getDimensionPixelSize(R.dimen.height_44dp);
        mBarLayout.setLayoutParams(LP);
        mActionBar = (ActionBarBackFragment)getSupportFragmentManager().findFragmentById(R.id.bar_fragment);
        mActionBarRightBtn = mActionBar.getTvBarRight();
        mActionBarRightBtn.setText(getResources().getString(R.string.action_save));
        mActionBarRightBtn.setVisibility(View.VISIBLE);
        mActionBarRightBtn.setAlpha(0.25f);
        mActionBar.getBackButton().setVisibility(View.GONE);
        mActionBar.getTvBarLeft().setVisibility(View.VISIBLE);
        mActionBar.getTvTitle().setText(mActionBarTitle);
    }

    //点击EditText以外的区域隐藏键盘
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (ComUtils.isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
        }
        return super.dispatchTouchEvent(ev);
    }
}
